package com.fengdi.everyday.middle;

import com.fengdi.publicObject.ListNode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class ModifiedList {
    public ListNode modifiedList(int[] nums, ListNode head) {
        if(head == null) return null;

        // 转换为set
        HashSet<Integer> set = new HashSet<>(Arrays.asList(
                Arrays.stream(nums)
                        .boxed()
                        .toArray(Integer[]::new)
        ));

        ListNode newHead = new ListNode();
        newHead.next = head;
        ListNode p = newHead;
        while(p.next != null) {
            if(set.contains(p.next.val)) {
                this.deleteNextNode(p);
            }else {
                p = p.next;
            }
        }

        return newHead.next;
    }

    // 删除该节点的下一个节点
    private void deleteNextNode(ListNode pre) {
        if(pre == null) return;
        pre.next = pre.next.next;
    }
}
